home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / Interface Classes / CPPVisualObject.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  8.4 KB  |  330 lines  |  [TEXT/KAHL]

  1. /***************************************************** IMPLEMENTATION
  2.     DATE:    9/22/93
  3.     AUTHOR: Eric R. Rosé
  4.  
  5.     CLASS:  CPPVisualObject
  6.     
  7.     SUPERCLASS: CPPObject
  8.     
  9.         This is an abstract C++ superclass for objects which live in
  10.         windows.
  11.     
  12. ********************************************************************/
  13.  
  14. #include <CPPVisualObject.h>
  15. #include <CPPWindow.h>
  16. #include <Commands.h>
  17.  
  18. Rect        kEmptyRect         =    {-1, -1, -1, -1};
  19. Rect        kDefaultRect     =    {0, 0, 10, 10};
  20. RGBColor    RGBBlack        =    {0, 0, 0},
  21.             RGBWhite        =    {65535, 65535, 65535},
  22.             RGBRed            =    {65535, 0, 0},
  23.             RGBGreen        =    {0, 65535, 0},
  24.             RGBBlue            =    {0, 0, 65535};
  25.  
  26.  
  27. /*-----------------------------------------------------------------*/
  28. /*------------------------ PUBLIC METHODS -------------------------*/
  29. /*-----------------------------------------------------------------*/
  30.  
  31.     CPPVisualObject::CPPVisualObject (CPPWindow *itsWindow, Rect *itsBounds,
  32.                                       Boolean canBeTarget,
  33.                                       Boolean active, Boolean visible)
  34.     {
  35.         if (itsWindow)
  36.           {
  37.               this->owningWObject = itsWindow;
  38.             this->owningWindow = itsWindow->GetWindow();
  39.             this->isActive = active;
  40.             this->isVisible = visible;
  41.             this->bounds = *itsBounds;
  42.             this->canBeTarget = canBeTarget;
  43.             this->isTarget = 0;
  44.             this->doClickProc = NULL;
  45.             this->commandEquivalent = kNoCommand;
  46.             // tell the window it is responsible for feeding us
  47.             itsWindow->StartManagingObject(this);
  48.           }
  49.     }
  50.  
  51. /*-----------------------------------------------------------------*/
  52.  
  53.     CPPVisualObject::~CPPVisualObject (void)
  54.     {
  55.         // tell the window it doesn't have to worry about us anymore
  56.         if (owningWObject)
  57.           owningWObject->StopManagingObject(this);
  58.     }
  59.         
  60. /*-----------------------------------------------------------------*/
  61.  
  62.     char    *CPPVisualObject::ClassName (void)
  63.     {
  64.         return "CVisualObject";
  65.     }
  66.  
  67. /*-----------------------------------------------------------------*/
  68.  
  69.     Boolean    CPPVisualObject::InColorWindow (void)
  70.     /* return TRUE if we live in a color window */
  71.     {
  72.         if (this->owningWObject)
  73.           return this->owningWObject->IsColorWindow();
  74.         else
  75.           return FALSE;
  76.     }
  77.  
  78. /*-----------------------------------------------------------------*/
  79.  
  80.     Boolean    CPPVisualObject::DoCommand (short commandID)
  81.     /* do a command associated with a menu */
  82.     /* SUBCLASS SHOULD OVERRIDE */
  83.     {
  84.         return FALSE;
  85.     }
  86.  
  87. /*-----------------------------------------------------------------*/
  88.  
  89.     void    CPPVisualObject::Draw (void)
  90.     /* method for drawing an object */
  91.     /* SUBCLASS SHOULD OVERRIDE */
  92.     {
  93.  
  94.     }
  95.  
  96. /*-----------------------------------------------------------------*/
  97.  
  98.     void    CPPVisualObject::DoIdle (void)
  99.     /* SUBCLASS SHOULD OVERRIDE */
  100.     {
  101.     
  102.     }
  103.  
  104. /*-----------------------------------------------------------------*/
  105.  
  106.     void    CPPVisualObject::SetCommand (short    theCommand)
  107.     {
  108.         this->commandEquivalent = theCommand;
  109.     }
  110.  
  111. /*-----------------------------------------------------------------*/
  112.  
  113.     void    CPPVisualObject::SetDoClickProc (ClickProcPtr clickProc)
  114.     {
  115.         this->doClickProc = clickProc;
  116.     }
  117.  
  118. /*-----------------------------------------------------------------*/
  119.  
  120.     Boolean    CPPVisualObject::DoClick (EventRecord *theEvent)
  121.     /* SUBCLASS SHOULD OVERRIDE */
  122.     {
  123.         return FALSE;
  124.     }
  125.  
  126. /*-----------------------------------------------------------------*/
  127.  
  128.     Boolean    CPPVisualObject::DoKey (char theKey, short modifiers, short what)
  129.     /* SUBCLASS SHOULD OVERRIDE */
  130.     {
  131.         return FALSE;
  132.     }
  133.  
  134. /*-----------------------------------------------------------------*/
  135.     
  136.     void    CPPVisualObject::Move (short newH, short newV)
  137.     /* move the entire object to a new position */
  138.     /* SUBCLASS SHOULD OVERRIDE */
  139.     {
  140.         RgnHandle    OldBoundsRgn = NewRgn(),    
  141.                     NewBoundsRgn = NewRgn();
  142.         
  143.         // remember where we were
  144.         RectRgn(OldBoundsRgn, GetBounds());
  145.     
  146.         if (this->isTarget)
  147.           EraseHilite();
  148.         
  149.         // call the virtual method to move us
  150.         MoveContent (newH, newV);
  151.         
  152.         // find out where we are
  153.         RectRgn(NewBoundsRgn, GetBounds());
  154.         
  155.         // erase and invalidate the XOR of the two regions
  156.         XorRgn (OldBoundsRgn, NewBoundsRgn, NewBoundsRgn);
  157.         EraseRgn (NewBoundsRgn);
  158.         InvalRgn (NewBoundsRgn);
  159.  
  160.         if (this->isTarget)
  161.           DrawHilite();
  162.     }
  163.  
  164. /*-----------------------------------------------------------------*/
  165.  
  166.     void    CPPVisualObject::Resize (short newWidth, short newHeight)
  167.     /* change the width and height of an object */
  168.     /* SUBCLASS SHOULD OVERRIDE */
  169.     {
  170.         RgnHandle    OldBoundsRgn = NewRgn(),    
  171.                     NewBoundsRgn = NewRgn();
  172.         
  173.         // remember where we were
  174.         RectRgn(OldBoundsRgn, GetBounds());
  175.         
  176.         if (this->isTarget)
  177.           EraseHilite();
  178.  
  179.         // call the virtual method to move us
  180.           ResizeContent(newWidth, newHeight);
  181.           
  182.         // find out where we are
  183.         RectRgn(NewBoundsRgn, GetBounds());
  184.  
  185.         // erase and invalidate the changed region
  186.         UnionRgn(NewBoundsRgn, OldBoundsRgn, NewBoundsRgn);
  187.         EraseRgn (NewBoundsRgn);
  188.         InvalRgn (NewBoundsRgn);
  189.  
  190.         if (this->isTarget)
  191.           DrawHilite();
  192.     }
  193.  
  194. /*-----------------------------------------------------------------*/
  195.  
  196.     void    CPPVisualObject::Global2Local (Point *thePoint)
  197.     /* convert a point from global coordinates to local coordinates */
  198.     /* regardless of whether the port is set to our window or not */
  199.     {
  200.         GrafPtr    savePort;
  201.         
  202.         GetPort (&savePort);
  203.         if (savePort != this->owningWindow)
  204.           {
  205.               SetPort (this->owningWindow);
  206.               GlobalToLocal (thePoint);
  207.               SetPort (savePort);
  208.           }
  209.         else
  210.           GlobalToLocal(thePoint);
  211.     }
  212.  
  213. /*-----------------------------------------------------------------*/
  214.  
  215.     void    CPPVisualObject::TargetHilite (Boolean makeTarget)
  216.     /* change the hiliteed state of the object (some objects, like */
  217.     /* lists, have different appearances when they are the target) */
  218.     /* SUBCLASS SHOULD OVERRIDE */
  219.     {
  220.         this->isTarget = makeTarget;
  221.     }
  222.  
  223. /*-----------------------------------------------------------------*/
  224.  
  225.     void    CPPVisualObject::DrawHilite (void)
  226.     /* Draw the hilite around an object, indicating it is the target */
  227.     /* SUBCLASS SHOULD OVERRIDE */
  228.     {
  229.  
  230.     }
  231.  
  232. /*-----------------------------------------------------------------*/
  233.  
  234.     void    CPPVisualObject::EraseHilite (void)
  235.     /* Remove the hilite around an object, indicating it is the target */
  236.     /* SUBCLASS SHOULD OVERRIDE */
  237.     {
  238.  
  239.     }    
  240.  
  241. /*-----------------------------------------------------------------*/
  242.  
  243.     void    CPPVisualObject::MakeVisible (Boolean nowVisible)
  244.     /* do the bookkeeping to record that we are now (in)visible */
  245.     /* SUBCLASS MAY OVERRIDE */
  246.     {
  247.         this->isVisible = nowVisible;
  248.     }
  249.  
  250. /*-----------------------------------------------------------------*/
  251.  
  252.     void    CPPVisualObject::Activate (Boolean nowActive)
  253.     /* do the bookkeeping to record that we are now (in)active */
  254.     {
  255.         this->isActive = nowActive;
  256.     }
  257.  
  258. /*-----------------------------------------------------------------*/
  259.  
  260.     Boolean    CPPVisualObject::InContent(Point clickPt)
  261.     /* return TRUE if the point (in global coordinates) is */
  262.     /* inside the object */
  263.     /* SUBCLASS SHOULD OVERRIDE */
  264.     {    
  265.         Point    myPoint = clickPt;
  266.         
  267.         if (this->isVisible)
  268.           {
  269.             Global2Local (&myPoint);
  270.             return PtInRect (myPoint, GetBounds());
  271.           }
  272.         else
  273.           return FALSE;
  274.     }
  275.  
  276. /*-----------------------------------------------------------------*/
  277.  
  278.     Rect    *CPPVisualObject::GetBounds (void)
  279.     /* return the boundsrect of the object */
  280.     /* SUBCLASS MUST OVERRIDE */
  281.     {
  282.         return &kEmptyRect;
  283.     }
  284.  
  285. /*-----------------------------------------------------------------*/
  286.  
  287.     Boolean    CPPVisualObject::IsActive (void)
  288.     /* return whether the object is active or inactive */
  289.     /* SUBCLASS MAY OVERRIDE */
  290.     {
  291.         return this->isActive;
  292.     }
  293.  
  294. /*-----------------------------------------------------------------*/
  295.  
  296.     Boolean    CPPVisualObject::IsVisible (void)
  297.     /* return whether the object is visible or invisible */
  298.     {
  299.         return this->isVisible;
  300.     }
  301.  
  302. /*-----------------------------------------------------------------*/
  303.  
  304.     Boolean    CPPVisualObject::CanBeMadeTarget (void)
  305.     /* return whether the object can be made the target of the window */
  306.     {
  307.         return this->canBeTarget;
  308.     }
  309.  
  310. /*-----------------------------------------------------------------*/
  311. /*------------------------ PUBLIC METHODS -------------------------*/
  312. /*-----------------------------------------------------------------*/
  313. /*-----------------------------------------------------------------*/
  314.     
  315.     void    CPPVisualObject::MoveContent (short newH, short newV)
  316.     /* move the contents of the object to a new position */
  317.     /* SUBCLASS MUST OVERRIDE */
  318.     {
  319.         
  320.     }
  321.  
  322. /*-----------------------------------------------------------------*/
  323.  
  324.     void    CPPVisualObject::ResizeContent (short newWidth, short newHeight)
  325.     /* change the size of the contents of the object */
  326.     /* SUBCLASS MUST OVERRIDE */
  327.     {
  328.         
  329.     }
  330.